package com.bfsoft.springbootinit.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bfsoft.springbootinit.config.SystemConfig;
import com.bfsoft.springbootinit.pojo.vo.ResultVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Objects;

@Component
@Slf4j
public class RestTemplateUtil {

	@Resource
	private RestTemplate restTemplate;

	@Resource
	public SystemConfig systemConfig;

	public ResultVO < Object > getListDataById ( String url , String id ) {
		ResultVO resultVO = null;
		try {
			resultVO = restTemplate.getForObject ( systemConfig.getProcessCenterUrl () + url , ResultVO.class , id );
			if ( "200".equals ( resultVO.getCode () ) ) {
				return ResultVO.ok ( resultVO.getData () );
			}
			else {
				return ResultVO.fail ( resultVO.getMessage () );
			}
		} catch ( Exception e ) {
			log.error ( "调用根据系统id获取部署流程列表接口出现异常" , e );
			return null;
		}
	}

	public ResultVO < Object > getDataById ( String url , String id ) {
		ResultVO resultVO = null;
		try {
			resultVO = restTemplate.getForObject ( systemConfig.getProcessCenterUrl () + url , ResultVO.class , id );
			if ( "200".equals ( resultVO.getCode () ) ) {
				return ResultVO.ok ( resultVO.getData () );
			}
			else {
				return ResultVO.fail ( resultVO.getMessage () );
			}
		} catch ( Exception e ) {
			log.error ( "调用根据系统id获取部署流程列表接口出现异常" , e );
			return null;
		}
	}


	public ResultVO < Object > postFetchData ( String url , Object object ) {
		ResultVO resultVO = null;
		try {
			resultVO = restTemplate.postForObject ( systemConfig.getProcessCenterUrl () + url , object ,
					ResultVO.class );
			if ( "200".equals ( resultVO.getCode () ) ) {
				return ResultVO.ok ( resultVO.getData () );
			}
			else {
				return ResultVO.fail ( resultVO.getMessage () );
			}
		} catch ( Exception e ) {
			log.error ( "调用根据系统id获取部署流程列表接口出现异常" , e );
			return null;
		}
	}


	public ResultVO < Object > postFetchDataList ( String url , Object object ) {
		ResultVO resultVO = null;
		try {
			resultVO = restTemplate.postForObject ( systemConfig.getProcessCenterUrl () + url , object ,
					ResultVO.class );
			if ( "200".equals ( resultVO.getCode () ) ) {
				return ResultVO.ok ( resultVO.getData () );
			}
			else {
				return ResultVO.fail ( resultVO.getMessage () );
			}
		} catch ( Exception e ) {
			log.error ( "调用根据系统id获取部署流程列表接口出现异常" , e );
			return null;
		}
	}


}
